import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { {{pascalCase name}}Service } from './{{kebabCase name}}.service';
import { {{pascalCase name}}Input } from './dto/{{kebabCase name}}.input.type';
import { {{pascalCase name}}Type } from './dto/{{kebabCase name}}.type';
import { GqlAuthGuard } from '@/common/guards/auth.guards';
import { UseGuards } from '@nestjs/common';
import {
  TEMP_DEL_FAIL,
  TEMP_NOT_EXIST,
  TEMP_FAIL,
  TEMP_CREATE_FAIL,
  SUCCESS,
  UPDATE_ERROR,
} from '@/common/constants/code';
import { CurUserId } from '@/common/decorators/current-user.decorator';
import { PageInput } from '@/common/dto/page.input';
import { {{pascalCase name}}Result, {{pascalCase name}}Results } from './dto/{{kebabCase name}}.output.type';
import { Result } from '@/common/dto/result.type';
import { {{pascalCase name}} } from './models/{{kebabCase name}}.entity';
import { FindOptionsWhere, Like } from 'typeorm';
import { CurOrgId } from '@/common/decorators/current-org.decorator';

@Resolver(() => {{pascalCase name}}Type)
@UseGuards(GqlAuthGuard) //启用守卫
export class {{pascalCase name}}Resolver {
  constructor(private readonly {{kebabCase name}}Service: {{pascalCase name}}Service) {}

  @Query(() => {{pascalCase name}}Result, { description: '使用ID查询用户' })
  async get{{pascalCase name}}Info(@Args('id') id: string): Promise<{{pascalCase name}}Result> {
    const result = await this.{{kebabCase name}}Service.findById(id);
    if (result) {
      return {
        code: SUCCESS,
        data: result,
        message: '获取成功',
      };
    }
    return {
      code: TEMP_NOT_EXIST,
      message: '用户不存在',
    };
  }
  // 将新增和修改合并到一个接口
  @Mutation(() => {{pascalCase name}}Result)
  async commit{{pascalCase name}}(
    @Args('params') params: {{pascalCase name}}Input,
    @CurUserId() userId: string,
    @CurOrgId() orgId: string,
    @Args('id', { nullable: true }) id?: string,
  ): Promise<Result> {
    if (!id) {
      // 如果没有，那就创建
      const res = await this.{{kebabCase name}}Service.create({
        ...params,
        createBy: userId,
        org: {
          id: orgId,
        },
      });
      if (res) {
        return {
          code: SUCCESS,
          message: '创建成功',
        };
      }
      return {
        code: TEMP_CREATE_FAIL,
        message: '操作失败',
      };
    }
    const {{kebabCase name}} = await this.{{kebabCase name}}Service.findById(id);
    if (!{{kebabCase name}}) {
      return {
        code: TEMP_NOT_EXIST,
        message: '用户信息不存在',
      };
    }
    const res = await this.{{kebabCase name}}Service.updateById(id, {
      ...params,
      updateBy: userId,
    });
    if (res) {
      return {
        code: SUCCESS,
        message: '更新成功',
      };
    }
    return {
      code: UPDATE_ERROR,
      message: '更新失败',
    };
    
  }
  // 获取多个实例
  @Query(() => {{pascalCase name}}Results)
  async get{{pascalCase name}}s(
    @Args('page') page: PageInput,
    @CurUserId() userId: string,
    @Args('name', { nullable: true }) name?: string,
  ): Promise<{{pascalCase name}}Results> {
    const { pageNum, pageSize } = page;
    const where: FindOptionsWhere<{{pascalCase name}}> = { createBy: userId };
    if (name) {
      where.name = Like(`%${name}%`);
    }
    const [results, total] = await this.{{kebabCase name}}Service.find{{pascalCase name}}s({
      start: (pageNum - 1) * pageSize,
      size: pageSize,
      where,
    });
    return {
      code: SUCCESS,
      data: results,
      page: {
        pageNum,
        pageSize,
        total,
      },
      message: '获取成功',
    };
  }
  // @Mutation(() => Boolean, { description: '新增' })
  // async create{{pascalCase name}}(@Args('params') params: {{pascalCase name}}Input): Promise<boolean> {
  //   return await this.{{kebabCase name}}Service.create(params);
  // }
  @Mutation(() => Result, { description: '删除' })
  async delete{{pascalCase name}}(
    @Args('id') id: string,
    @CurUserId() userId: string,
  ): Promise<Result> {
    const result = await this.{{kebabCase name}}Service.findById(id);
    if (result) {
      const delRes = await this.{{kebabCase name}}Service.deleteById(id, userId);
      if (delRes) {
        return {
          code: SUCCESS,
          message: '删除成功',
        };
      }
      return {
        code: TEMP_DEL_FAIL,
        message: '删除失败',
      };
    }
    return {
      code: TEMP_FAIL,
      message: '用户不存在',
    };
  }
  //   @Query(() => {{pascalCase name}}Type, { description: '使用ID查询用户' })
  //   async find(@Args('id') id: string): Promise<{{pascalCase name}}Type> {
  //     return await this.{{kebabCase name}}Service.findById(id);
  //   }
  //   @Mutation(() => Boolean, { description: '更新用户' })
  //   async update(
  //     @Args('id') id: string,
  //     @Args('params') params: {{pascalCase name}}Input,
  //   ): Promise<boolean> {
  //     return await this.{{kebabCase name}}Service.update(id, params);
  //   }
  //   @Mutation(() => Boolean, { description: '删除用户' })
  //   async del(@Args('id') id: string): Promise<boolean> {
  //     return await this.{{kebabCase name}}Service.del(id);
  //   }
}
